package com.google.code.p.odbcconnectionchecker;

import java.awt.EventQueue;
import java.io.File;
import java.io.Serializable;

import com.google.code.p.odbcconnectionchecker.db.ConnectionCheckerDbTalker;
import com.google.code.p.odbcconnectionchecker.db.ConnectionStatus;

public class ConnectionCheckerMenuItemJdbc implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String connectionDriver; //Contains the Class.forName value
	private String connectionURL; //Contains the connection String jdbc:
	private String username; //Contains the username for login into the database 
	private String password; //Contains the password for login into the database
	private String driverURL; //Contains the path to the jdbc jar driver
	
	private boolean useDriver=false;
	
	private ConnectionMenu myCm=null;
	
	PeriodicChecker pc = new PeriodicChecker();;

	public ConnectionCheckerMenuItemJdbc() { 
		startChecking();
	}
	
	public ConnectionCheckerMenuItemJdbc (
			String connectionDriver, String connectionURL, 
			String username, String password, String driverURL) {
		setConnectionDriver(connectionDriver);
		setConnectionURL(connectionURL); 
		setUsername(username); 
		setPassword(password);
		setDriverURL(driverURL); 
		
		startChecking();
	}
	
	public void startChecking () { 
		pc.setRefreshPeriod(myCm.getRefreshTimeSec());
		pc.setCj(this);
		Thread t = new Thread(pc);
        t.start();		
	}
	
	public void stopChecking () { 
		pc.setStop(true);
	}
	
	public void checkConnection () { 
		ConnectionStatus cs = ConnectionCheckerDbTalker.getConnectionStatus(this); 
		myCm.addNewConnectionStatus(cs);
	}

	public String getConnectionDriver() {
		return connectionDriver;
	}

	public void setConnectionDriver(String connectionDriver) {
		this.connectionDriver = connectionDriver;
	}

	public String getConnectionURL() {
		return connectionURL;
	}

	public void setConnectionURL(String connectionURL) {
		this.connectionURL = connectionURL;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDriverURL() {
		return driverURL;
	}

	public void setDriverURL(String driverURL) {
		//Check if the driver file exists
		File file = new File (driverURL);
		if (file.exists() && file.isFile()) { 
			this.driverURL = driverURL;
			this.setUseDriver(true);
		} else {
			this.driverURL = "";
			this.setUseDriver(false);
		}
	}

	public boolean isUseDriver() {
		return useDriver;
	}

	public void setUseDriver(boolean useDriver) {
		this.useDriver = useDriver;
	}

	public ConnectionMenu getMyCm() {
		return myCm;
	}

	public void setMyCm(ConnectionMenu myCm) {
		this.myCm = myCm;
	}	
}
